﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Imaginecup.sprite;
using imaginecup.util;
using Imaginecup.util.resourceMan;
using Imaginecup.util;

namespace Imaginecup.gameState.ending.scene
{
    public class EndingFadeScene:IScene
    {
        private ISprite m_Sprite;
        private EntityInfo m_Info;
        private IFadeLoadContent m_LoadContent;
        private FadeHelper m_FadeHelper;

        private IScene m_BackGroundSceneAtFadeIn;
        private int m_FadeInTime;

        private IScene m_BackGroundSceneAtFadeOut;
        private int m_FadeOutTime;

        private IScene m_CurrentBackScene;
        private RenderMode m_CurrentMode;

        private bool m_bComplete;

        public EndingFadeScene()
        {
            m_Info = new EntityInfo(0, 0, 800, 480);
            m_bComplete = false;
        }
            
        public void SetFadeInBackScene(IScene backScene , int delayTime)
        {
            m_BackGroundSceneAtFadeIn = backScene;
            m_FadeInTime = delayTime;
        }

        public void SetFadeOutBackScene(IScene backScene, int delayTime)
        {
            m_BackGroundSceneAtFadeOut = backScene;
            m_FadeOutTime = delayTime;
        }

        public void SetLoadEndingScene(ILoadContent content)
        {
            m_LoadContent = (IFadeLoadContent)content;
        }

        public void LoadEndingScene()
        {
            m_FadeHelper = m_LoadContent.LoadFadeHelper();
            m_Sprite = m_LoadContent.LoadSprite();

            m_FadeHelper.SetTime(m_FadeOutTime);
            m_CurrentMode = RenderMode.FADE_OUT;
            m_FadeHelper.SetFadeMode(RenderMode.FADE_OUT);

            m_CurrentBackScene = m_BackGroundSceneAtFadeOut;
        }

        public void UnloadEndingScene()
        {
            m_Sprite.Release();
            m_LoadContent.UnloadResource();
        }

        public void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            m_CurrentBackScene.Update(gameTime);
            m_FadeHelper.Update(gameTime);

            if( m_FadeHelper.CompleteRender() && m_CurrentMode == RenderMode.FADE_IN)
            {
                m_bComplete = true;
            }
            else if (m_FadeHelper.CompleteRender() && m_CurrentMode == RenderMode.FADE_OUT)
            {
                m_CurrentMode = RenderMode.FADE_IN;
                m_FadeHelper.SetFadeMode(m_CurrentMode);
                m_FadeHelper.SetTime(m_FadeInTime);
                m_CurrentBackScene = m_BackGroundSceneAtFadeIn;
            }
        }

        public void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch)
        {
            m_CurrentBackScene.Draw(spriteBatch);
            m_Sprite.Draw(spriteBatch, m_Info);
        }

        public bool IsComplete()
        {
            return m_bComplete;
        }
    }
}
